﻿<?xml version="1.0" encoding="utf-8" ?>

<cls:class
	name="Collection"
	type="http://metalx.org/Collection"
	static="true"
	xmlns="http://metalx.org/Program"
	xmlns:dbg="http://metalx.org/i386/Functions/Debug"
	xmlns:lst="http://metalx.org/i386/Functions/List"
	xmlns:con="http://metalx.org/i386/Functions/Console"
	xmlns:int="http://metalx.org/i386/Functions/Integer"
	xmlns:str="http://metalx.org/i386/Functions/String"
	xmlns:clsf="http://metalx.org/i386/Functions/Class"
	xmlns:var="http://metalx.org/Variable"
	xmlns:sys="http://metalx.org/i386/Functions/System"
	xmlns:mm="http://metalx.org/i386/Functions/MemoryManager"
	xmlns:cat="http://metalx.org/i386/Functions/Catalog"
	xmlns:obj="http://metalx.org/i386/Functions/Object"
	xmlns:asc="http://metalx.org/Ansi/Ascii"
	xmlns:logic="http://metalx.org/Intel/80386/Logic"
	xmlns:shift="http://metalx.org/Intel/80386/Shift"
	xmlns:index="http://metalx.org/Intel/80386/Index"
	xmlns:inc="http://metalx.org/Intel/80386/Increment"
	xmlns:math="http://metalx.org/Intel/80386/Math"
	xmlns:imm="http://metalx.org/Intel/80386/Immediate"
	xmlns:cpu="http://metalx.org/Intel/80386/Operators"
	xmlns:op="http://metalx.org/Intel/80386/Operands"
	xmlns:cls="http://metalx.org/Class">

	<cls:field name="Count" offset="0" type="http://metalx.org/Integer"/>
	<cls:field name="Items" offset="4" type="http://metalx.org/Reference"/>

	<cls:method name="Create" type="http://metalx.org/Collection" static="true">
		<cpu:PushBXToStack/>
		
		<lst:Create length="8"/>

		<lst:CreateObject/>

		<cpu:PushDIToStack/>

		<mm:CheckOut length="8"/>

		<cpu:CopyImmediateToOperandFunction/>
		<imm:CopyImmediateToDIAddress/>
		<int>0</int>

		<cpu:PullAXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:PullBXFromStack/>

		<obj:Create length="8"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="Add" type="http://metalx.org/Collection">
		<cls:parameter name="Item" type="http://metalx.org/Object"/>

		<cpu:PushDIToStack/>

		<!--Increment Count-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:IncrementFunction/>
		<inc:IncrementSIAddress/>

		<!--Ensure Capacity-->
		<cpu:PushSIToStack/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddress/>

		<obj:GetData/>

		<cpu:CompareRegisterToOperand/>
		<op:AX-SIAddress/>

		<cpu:BranchToRelative8IfNotGreater/>
		<addressOf ref="notFull" type="Relative8"/>

		<cpu:CallRelative/>
		<addressOf ref="expand" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<label id="notFull"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullDIFromStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullSIFromStack/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddress/>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-IndexAddress/>
		<index:AXTimesFourPlusSI/>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>

		<label id="expand"/>

		<cpu:DecrementAX/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>

		<cpu:ShiftRegisterByOneFunction/>
		<shift:ShiftAXRegisterLeft/>

		<lst:Create/>

		<cpu:PushDIToStack/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSIRegister/>
		<byte>4</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToDIRegister/>
		<byte>4</byte>

		<cpu:Repeat/>
		<cpu:CopySIAddressToDIAddressAndIncrementSIAndDI/>

		<cpu:PullDIFromStack/>

		<lst:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="RemoveAt" type="http://metalx.org/Collection">
		<cls:parameter name="Index" type="http://metalx.org/Integer"/>

		<cpu:PushDIToStack/>

		<!--Decrement Count-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:IncrementFunction/>
		<inc:DecrementSIAddress/>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="notEmpty" type="Relative8"/>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>

		<label id="notEmpty"/>

		<cpu:CopyOperandToRegister/>
		<op:CX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:PushSIToStack/>

		<!--Get Index-->
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<!--Move Remaining Items-->
		<cpu:SubtractRegisterFromOperand/>
		<op:AX-CXRegister/>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="notLast" type="Relative8"/>

		<cpu:PullDIFromStack/>
		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>

		<label id="notLast"/>

		<cpu:PullDIFromStack/>

		<cpu:IncrementAX/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterLeft/>
		<byte>2</byte>

		<cpu:AddRegisterToOperand/>
		<op:AX-DIRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSIRegister/>
		<byte>4</byte>

		<cpu:Repeat/>
		<cpu:CopySIAddressToDIAddressAndIncrementSIAndDI/>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="Skip" type="http://metalx.org/CollectionIterator">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullSIFromStack/>
		
		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:PushAXToStack/>
		<cpu:PushDIToStack/>

		<!--<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>-->

		<sys:GetCatalog/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="className" type="Absolute32"/>

		<cat:FindType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-BXRegister/>

		<cpu:CopyImmediateToAX/>
		<int>8</int>

		<mm:CheckOut/>

		<cpu:PullDXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DX-DIAddress/>

		<cpu:PullDXFromStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DX-DIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:Create/>

		<cpu:ReturnToNearCaller/>

		<var:string id="className">http://metalx.org/CollectionIterator</var:string>

	</cls:method>

	<cls:method name="First" type="http://metalx.org/Object">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="Last" type="http://metalx.org/Object">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DI-IndexAddress/>
		<index:AXTimesFourPlusSI/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetCapacity" type="http://metalx.org/Integer">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<int:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ToString" type="http://metalx.org/String">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddress/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterLeft/>
		<byte>4</byte>

		<str:Create/>

		<cpu:PushDIToStack/>

		<cpu:BranchToRelative8IfCXIsZero/>
		<addressOf ref="createObject" type="Relative8"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:GetData/>
		
		<label id="forEachItem"/>
		
		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSIRegister/>
		<byte>4</byte>

		<cpu:CallRelative/>
		<addressOf ref="appendItem" type="Relative32"/>
		
		<cpu:LoopToRelative8/>
		<addressOf ref="forEachItem" type="Relative8"/>

		<label id="createObject"/>

		<cpu:PullDIFromStack/>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>

		
		
		<label id="appendItem"/>

		<cpu:PushSIToStack/>
		<cpu:PushCXToStack/>
		
		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddress/>
		
		<cpu:PushSIToStack/>
		
		<obj:GetType/>

		<cpu:CopyImmediateToDI/>
		<addressOf ref="toString" type="Absolute32"/>

		<clsf:FindMethod/>
		
		<clsf:GetMethodEntryPoint/>

		<cpu:PullDIFromStack/>

		<cpu:LogicFunction/>
		<logic:CallSIRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>12</byte>

		<str:AppendString/>

		<cpu:CopyOperandToRegister/>
		<op:AX-IndexAddress/>
		<index:SP/>

		<cpu:CompareAXToImmediate/>
		<int>1</int>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="skipDelimiter" type="Relative8"/>

		<cpu:CopyImmediateToAL/>
		<asc:CarriageReturn/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>12</byte>

		<str:AppendCharacter/>

		<cpu:CopyImmediateToAL/>
		<asc:LineFeed/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>12</byte>

		<str:AppendCharacter/>

		<label id="skipDelimiter"/>
		
		<cpu:PullCXFromStack/>
		<cpu:PullSIFromStack/>

		<cpu:ReturnToNearCaller/>

		<var:string id="toString">ToString</var:string>
		<!--<label id="methodName"/>
		<int>8</int>
		<string>ToString</string>-->
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class" static="true">
		<cpu:CopyRegisterToOperand/>
		<op:BX-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>
</cls:class>